JavaScript is one of the most popular programming languages in the world. It can do a lot and have some features that are ahead of many other languages.
In this article, we’ll look at some good parts of JavaScript, including expressions, literals, and functions.
Expressions
Expressions are basic building blocks of JavaScript. They’re different from statements in that they return a value.
The simplest expressions are literal values, a variable, or built-in values.
Invocation of constructors with the new
keyword are also expressions.
An expression can be wrapped in parentheses, preceded by a prefix operator, or it can be followed by the following thing:
- infix operator and another expression
?
ternary operator followed by another expression- invocation
- refinement
The ternary operator takes 3 operands. The first is the condition before the question mark.
Then the expression that’s returned when the condition is truthy, followed by a colon. Then that’s followed by an expression if the condition is falsy.
Operators are evaluated in the following order:
. [] ()
(refinement and invocation)delete new typeof + — !
(unary operators)* / %
(multiplication, division, and remainder)+ 0
(addition / concatenation and subtraction)>= <= > <
(inequality)&&
(logical and)||
(logical or)?:
(ternary)
The typeof
operator can return 'number'
, 'string'
, 'boolean'
, 'undefined'
, 'function'
, and 'object'
.
If the operand is an array of null
, then 'object'
is retuned.
This is wrong.
If the operand of !
is truthy, then false
is returned. Otherwise, true
is returned.
The +
can add or concatenate. If we want to add, then make sure both operands are numbers.
The /
operator can return noninteger results even if both operands are integers.
The &&
operator returns the value of the first operand if the first operand is falsy.
Otherwise, it returns the value of the 2nd operand.
The ||
operator returns the value of the first operand if the first operand is truthy.
Otherwise, it produces the value of the 2nd operand.
Invocation causes the execution of the function value. The operator is a pair of parentheses that follow the function value.
Arguments can be contained in the parentheses.
For instance, the following:
const sum = add(1, 2, 3);
The parentheses after the add
is the invocation operator. and 1, 2, and 3 are the arguments.
The refinement operator is used to specify a property or element of an object or an array.
For instance, if we have:
const arr = [1, 2, 3];
Then we can use the refinement operator as follows:
const first = arr[0];
The square brackets form the refinement operator.
Likewise, we can access the property of an object with the refinement operator as follows:
const foo = obj['foo'];
We can get the foo
property from obj
.
It’s the only way to get a property of an object if the property name isn’t a valid identifier.
Literals
Object literals are great for specifying new objects.
We can specify properties as names or strings.
They’re treated as literal names and not variable names.
Therefore, the names of properties must be known at compile time.
But string property names can be added or removed at run time.
Array literals are great for specifying new arrays.
Functions
We can define functions with function literals.
It can have a name, which is optional. With it, we can call it recursively.
The body can include variable definitions and statements.
There’re 2 ways to define functions. We can define an arrow function or a traditional function with the function
keyword.
To define an arrow function, we can write:
(a, b) => {
// do something with a and b
}
To create a named arrow function, we can write:
const foo = (a, b) => {
// do something with a and b
}
Then we can call it by writing foo(1, 2)
.
To create traditional functions, we can use the function
keyword.
For instance, we can write:
function foo(a, b){
// do something with a and b
}
We can also create an anonymous function by writing:
function (a, b){
// do something with a and b
}
Conclusion
Functions are useful for storing statements and variables which we can run multiple times.
Expressions are code that returns a value. It can have various operators to produce the values that we need.
Object literals are used for creating objects in a convenient way. It’s used for creating one-off objects.